package leetcode.easy.yanghuitriangle;

import util.annotation.Solved;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <a href="https://leetcode.cn/problems/pascals-triangle/">链接</a>
 */
@Solved
public class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ans = new ArrayList<>();
        // lv1
        ans.add(Collections.singletonList(1));
        if (numRows == 1) {
            return ans;
        }

        for (int i = 0; i < numRows - 1; i++) {
            List<Integer> newRow = new ArrayList<>();
            newRow.add(1);
            List<Integer> preRow = ans.get(ans.size() - 1);
            for (int j = 0; j < preRow.size() - 1; j++) {
                newRow.add(preRow.get(j) + preRow.get(j + 1));
            }
            newRow.add(1);
            ans.add(newRow);
        }

        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().generate(4));
    }
}
